
#include "first_title.h"
#include "pt_fs.h"
#include "com_title.h"
#include "pt_dev.h"
#include "sys_font.h"

static first_title_t first_title;

static uint8_t fir_title_left  = FIRST_PAGE_DOWNLOAD;    //左边名字
static uint8_t fir_title_middle = 1;  //中间小点
static uint8_t fir_title_right = 1;  //右上角数字


// 与 first_title_page_e 对应
static char * first_title_name[FIRST_PAGE_MAX] = 
{
    " ",
    "联 网 | 下 载 美 居",
    "联 网 | 添 加 设 备",
    "联 网 | 配 网 中",
    "联 网 | 授 权 中",
    "联 网",
    "用 户 协 议 | 隐 私 条 款",
    "新 手 指 引"
};

int first_title_set(uint8_t left_val, uint8_t middle_val, uint8_t right_val)
{
    if(left_val >= FIRST_PAGE_MAX)
    {
        fir_title_left = FIRST_PAGE_HIGH;
        return 0;
    }

    fir_title_left   = left_val;
    fir_title_middle = middle_val;
    fir_title_right  = right_val;
    return 0;
}

uint8_t first_left_get(void)
{
    return fir_title_left;
}

char * first_left_name_get(void)
{
    return first_title_name[fir_title_left];
}

uint8_t first_middle_get(void)
{
    return fir_title_middle;
}

uint8_t first_right_get(void)
{
    return fir_title_right;
}

void timer_first_title(lv_timer_t * timer)
{
    // debug_d("%s,%d,%d,%d\n",first_left_name_get(),first_left_get(),first_middle_get(),first_right_get());
    if(FIRST_PAGE_HIGH != first_left_get())
    {
        lv_label_set_text(first_title.title_left, first_left_name_get());
        if(FIRST_PAGE_GUIDE != first_left_get())
        {
            first_title_left_show();
            first_title_middle_hide();
            first_title_right_label_show();
            first_title_right_btn_hide();

            lv_label_set_text_fmt(first_title.title_right, "%d/5", first_right_get());
        }
        else
        {
            first_title_left_show();
            first_title_middle_show();
            first_title_right_label_hide();
            first_title_right_btn_show();

            lv_img_set_src(first_title.top_point1, pt_fs_get(title_point[0]));
            lv_img_set_src(first_title.top_point2, pt_fs_get(title_point[0]));
            lv_img_set_src(first_title.top_point3, pt_fs_get(title_point[0]));
            lv_img_set_src(first_title.top_point4, pt_fs_get(title_point[0]));
            if(1 == first_middle_get())
            {
                lv_img_set_src(first_title.top_point1, pt_fs_get(title_point[1]));
            }
            else if(2 == first_middle_get())
            {
                lv_img_set_src(first_title.top_point2, pt_fs_get(title_point[1]));
            }
            else if(3 == first_middle_get())
            {
                lv_img_set_src(first_title.top_point3, pt_fs_get(title_point[1]));
            }
            else if(4 == first_middle_get())
            {
                lv_img_set_src(first_title.top_point4, pt_fs_get(title_point[1]));
            }
        }
    }
    else
    {
        first_title_left_hide();
        first_title_middle_hide();
        first_title_right_label_hide();
        first_title_right_btn_hide();
    }
}

void first_title_left(void)
{
    first_title.title_left = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(first_title.title_left, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(first_title.title_left,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(first_title.title_left,LV_OPA_80,0);
    lv_label_set_text(first_title.title_left," ");
    lv_obj_align(first_title.title_left,LV_ALIGN_TOP_LEFT,5,9);
    // debug_d("title left\n");

    first_title.first_title_timer = lv_timer_create(timer_first_title, 300,  NULL);
}

void first_title_middle()
{
    first_title.top_point1 = lv_img_create(lv_layer_top());
    lv_img_set_src(first_title.top_point1, pt_fs_get(title_point[0]));
    lv_obj_align(first_title.top_point1,LV_ALIGN_TOP_MID,-21,18);
    
    first_title.top_point2 = lv_img_create(lv_layer_top());
    lv_img_set_src(first_title.top_point2, pt_fs_get(title_point[0]));
    lv_obj_align(first_title.top_point2,LV_ALIGN_TOP_MID,-7,18);
    
    first_title.top_point3 = lv_img_create(lv_layer_top());
    lv_img_set_src(first_title.top_point3, pt_fs_get(title_point[0]));
    lv_obj_align(first_title.top_point3,LV_ALIGN_TOP_MID,7,18);
    
    first_title.top_point4 = lv_img_create(lv_layer_top());
    lv_img_set_src(first_title.top_point4, pt_fs_get(title_point[0]));
    lv_obj_align(first_title.top_point4,LV_ALIGN_TOP_MID,21,18);
}

void first_title_right(void)
{
    first_title.title_right = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(first_title.title_right, lv_font_get(18), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(first_title.title_right,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(first_title.title_right,LV_OPA_80,0);
    lv_obj_set_pos(first_title.title_right,315,9);
    lv_label_set_text_fmt(first_title.title_right, "%d/5", first_right_get());
    lv_obj_align( first_title.title_right,LV_ALIGN_TOP_RIGHT,-34,9);

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style,1);
    lv_style_set_border_color(&style, lv_color_make(0x26, 0x7A, 0xFF));
}

int first_title_create(void)
{
    first_title_left();
    first_title_middle();
    first_title_right();

    first_title_middle_hide();
    first_title_right_btn_hide();

    return 0;
}

int first_title_left_hide(void)
{
    if(first_title.title_left){
        lv_obj_add_flag(first_title.title_left,LV_OBJ_FLAG_HIDDEN);
    }
    return 0;
}

int first_title_left_show(void)
{
    if(first_title.title_left && lv_obj_has_flag(first_title.title_left,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.title_left,LV_OBJ_FLAG_HIDDEN);
    }
    return 0;
}

int first_title_middle_hide(void)
{
    if(first_title.top_point1){
        lv_obj_add_flag(first_title.top_point1,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point2){
        lv_obj_add_flag(first_title.top_point2,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point3){
        lv_obj_add_flag(first_title.top_point3,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point4){
        lv_obj_add_flag(first_title.top_point4,LV_OBJ_FLAG_HIDDEN);
    }

    return 0;
}

int first_title_middle_show(void)
{
    if(first_title.top_point1 && lv_obj_has_flag(first_title.top_point1,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.top_point1,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point2 && lv_obj_has_flag(first_title.top_point2,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.top_point2,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point3 && lv_obj_has_flag(first_title.top_point3,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.top_point3,LV_OBJ_FLAG_HIDDEN);
    }

    if(first_title.top_point4 && lv_obj_has_flag(first_title.top_point4,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.top_point4,LV_OBJ_FLAG_HIDDEN);
    }
    
    return 0;
}

int first_title_right_label_hide(void)
{
    if(first_title.title_right){
        lv_obj_add_flag(first_title.title_right,LV_OBJ_FLAG_HIDDEN);
    }

    return 0;
}

int first_title_right_label_show(void)
{
    if(first_title.title_right && lv_obj_has_flag(first_title.title_right,LV_OBJ_FLAG_HIDDEN)){
        lv_obj_clear_flag(first_title.title_right,LV_OBJ_FLAG_HIDDEN);
    }

    return 0;
}

int first_title_right_btn_hide(void)
{

    return 0;
}

int first_title_right_btn_show(void)
{

    return 0;
}

void first_title_hide(void)
{
    first_title_left_hide();
    first_title_middle_hide();
    first_title_right_label_hide();
    first_title_right_btn_hide();

    if(first_title.first_title_timer){
        lv_timer_pause(first_title.first_title_timer);
    }
}





